package com.fingard.dsp.bank.directbank.citic02;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.ICertificateMailedMsg;
import com.fingard.dsp.bank.reqrespobj.certificatemailmsg.ReqCertificateMailedMsgQry;
import com.fingard.dsp.bank.reqrespobj.certificatemailmsg.RetAtsCertificateMailedMsg;
import com.fingard.io.FileHelper;
import com.fingard.util.DateHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.text.DateFormat;
import java.util.Date;

/**
 * Created by F on 2019/9/29.
 */
public class CITIC02CertificateMailedMsg extends CITIC02Base implements ICertificateMailedMsg {
    @Override
    public RetAtsCertificateMailedMsg getCertificateMailedMsg(ReqCertificateMailedMsgQry qry) throws Exception {
        RetAtsCertificateMailedMsg retAts = new RetAtsCertificateMailedMsg(qry.reqHeader);
        retAts.writeStart();
        try {
            CITIC02Header header =  new CITIC02Header();
            header.stdprocode = "RLTMSG";
            header.msgsendertrc = reqHeader.transSeqID;
            header.trdcode = "CPCERMIQ";
//            封装body内容
            XmlTextWriter xmlWriterBody = new XmlTextWriter();
            xmlWriterBody.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            xmlWriterBody.writeStartElement("stream");
//            BRANDCD	品牌编码	VARCHAR	10	N	区分不同品牌（网络）
            xmlWriterBody.writeElementString("BRANDCD","");
//            SERIALNO	流水号	VARCHAR	20	Y	企业端交易流水号，不可重复
            xmlWriterBody.writeElementString("SERIALNO",reqHeader.transSeqID);
//            FIELD1	属性1	VARCHAR	30	N
            xmlWriterBody.writeElementString("FIELD1","");
//            FIELD2	属性2	VARCHAR	60	N
            xmlWriterBody.writeElementString("FIELD2","");
//            FIELD3	属性3	VARCHAR	100	Y	邮寄总数量
            xmlWriterBody.writeElementString("FIELD3",qry.MailNum);
//            CPCUSTCD	企业端经销商客户代码	VARCHAR	100	Y	 长安汽车新增
            xmlWriterBody.writeElementString("CPCUSTCD",qry.DealerCode);
//            LONCPNM	借款企业名称	VARCHAR	100	Y	 长安汽车新增
            xmlWriterBody.writeElementString("LONCPNM",qry.DealerName);

//            LIST	汽车列表	LIST
            xmlWriterBody.writeStartElement("LST");
            int index =0;
            while (qry.readRecord()) {
                xmlWriterBody.writeStartElement("row");
                index++;
//                DATANO	序号	VARCHAR	10	Y	数据序号，从1开始，依次递增
                xmlWriterBody.writeElementString("DATANO",index+"");
//                ZZJBH	主机厂编号	VARCHAR	6	N
                xmlWriterBody.writeElementString("ZZJBH","");
//                ZPPBH	品牌编号	VARCHAR	8	N
                xmlWriterBody.writeElementString("ZPPBH","");
//                VBELN	SON号	VARCHAR	10	Y	上送“厂家ERP订单号”
                xmlWriterBody.writeElementString("VBELN",qry.reqDetail.OrderCode);
//                SERNR	VIN码	VARCHAR	18	Y	上传“车架号”
                xmlWriterBody.writeElementString("SERNR",qry.reqDetail.VIN);
//                RCPNO	回执单号	VARCHAR	17	N
                xmlWriterBody.writeElementString("RCPNO","");
//                EXPNO	快递单号	VARCHAR	20	Y	邮寄单号
                xmlWriterBody.writeElementString("EXPNO",qry.reqDetail.MailNo);
//                ZEUGN	合格证号	VARCHAR	30	Y
                xmlWriterBody.writeElementString("ZEUGN",qry.reqDetail.CertificationNo);
//                DTMAIL	邮寄日期	VARCHAR	8	Y	“YYYYMMDD”上送“邮寄时间”
                xmlWriterBody.writeElementString("DTMAIL", qry.reqDetail.MailTime.substring(0,8));
//                TMMAIL	邮寄时间	VARCHAR	6	Y	“HHMMSS”上送“邮寄时间”
                xmlWriterBody.writeElementString("TMMAIL", qry.reqDetail.MailTime.substring(8));
//                USRMAIL	邮寄人	VARCHAR	12	Y	快递公司名称
                xmlWriterBody.writeElementString("USRMAIL",qry.reqDetail.MailCompanyName);
//                ZMAR	备注	VARCHAR	300	N	备注
                xmlWriterBody.writeElementString("ZMAR","");
//                UDATE	更新日期	VARCHAR	8	N	“YYYYMMDD”
                xmlWriterBody.writeElementString("UDATE","");
//                UTIME	更新时间	VARCHAR	6	N	“HHMMSS”
                xmlWriterBody.writeElementString("UTIME","");
//                FIELD4	属性4	VARCHAR	30	N
                xmlWriterBody.writeElementString("FIELD4","");
//                FIELD5	属性5	VARCHAR	60	N
                xmlWriterBody.writeElementString("FIELD5","");
//                FIELD6	属性6	VARCHAR	100	N
                xmlWriterBody.writeElementString("FIELD6","");

                xmlWriterBody.writeEndElement();
            }
//                    LIST_END
            xmlWriterBody.writeEndDocument();
            String[] tmpStrRet = sendToBank2(xmlWriterBody.toXmlString(),header);
            if(tmpStrRet[0].length() == 0) {
                StringBuilder tmpbkMsg = new StringBuilder();
                //解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpbkMsg);
                if (tmpBkReader != null) {
                    String retCode =tmpBkReader.getFirstText("/stream/RETCODE");
                    if("AAAAAAA".equals(retCode)){
                        boolean result = false;
                        while (tmpBkReader.read("/stream/list/row")){
                            if("S".equals(tmpBkReader.readedXElement.getSingleText("FLAG"))){
                                result = true;
                            }else {
                                result = false;
                                retAts.respInfo = tmpBkReader.readedXElement.getSingleText("MESSAGE");
                                break;
                            }
                        }
                        if(result){
                            retAts.respCode = "0";
                        }else{
                            retAts.respCode = "-1";
                        }
                    }else{
                        retAts.respCode = "-1";
                        retAts.respInfo = retCode;
                    }
                }else {
                    throw new Exception(tmpBkReader.toString());
                }
            } else{
                throw new Exception(tmpStrRet[0]);
            }
        }catch (Exception ex){
            ex.getStackTrace();
            retAts.respCode ="-1";
            retAts.respInfo = ex.getMessage();
        }
        retAts.writeRespCode(retAts.respCode);
        retAts.writeRespInfo(retAts.respInfo);
        retAts.writeEnd();
        return retAts;
    }
}
